package com.yyt.module_main.act;

import android.content.Intent;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RadioGroup;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.vondear.rxtool.RxImageTool;
import com.yyt.libsrc.bean.SubordinateListBean;
import com.yyt.libsrc.conf.Consts;
import com.yyt.libsrc.helper.LocalHelper;
import com.yyt.libsrc.net.JavaParamsBuilder;
import com.yyt.libsrc.net.NetUtil;
import com.yyt.libsrc.net.SuccessCallback;
import com.yyt.libsrc.router.RouteTable;
import com.yyt.libsrc.router.RouterHelper;
import com.yyt.module_main.R;
import com.yyt.module_main.databinding.ActivityMemberListBinding;
import com.yyt.module_main.databinding.ItemMemberListAgentBinding;
import com.yyt.module_main.databinding.ItemMemberListBinding;
import com.yyt.module_main.vm.MemberListVM;

import top.lixb.libcommon.base.ItemHolder;
import top.lixb.libcommon.base.RefreshableActivity;
import top.lixb.libcommon.base.SimpleBindingAdapter;
import top.lixb.libcommon.helper.GlideHelper;
import top.lixb.libcommon.utils.StringUtils;

@Route(path = RouteTable.main_member_list)
public class MemberListActivity extends RefreshableActivity<ActivityMemberListBinding> {

    private MemberListVM mVm;
    private SimpleBindingAdapter<SubordinateListBean.DataBean.ListBean, ItemMemberListAgentBinding> mAdapter;
    private SimpleBindingAdapter<SubordinateListBean.DataBean.ListBean, ItemMemberListBinding> mMemberAdapter;
    private String mSid;
    private boolean mShowsublist;

    @Override
    protected void init() {
        mVm = new MemberListVM();
        mRefreshContentBinding.setVm(mVm);
        if (getIntent() != null && getIntent().getExtras() != null) {
            mShowsublist = getIntent().getBooleanExtra("showsublist", false);
            int role = getIntent().getIntExtra("role", 1);
            if (mShowsublist && role != 3) {
                mRefreshContentBinding.llGroup.setVisibility(View.GONE);
            } else {
                mRefreshContentBinding.llGroup.setVisibility(View.VISIBLE);
                if (role == 2) {
                    mRefreshContentBinding.rb1.setVisibility(View.GONE);
                    mRefreshContentBinding.rb1.setChecked(false);
                    mRefreshContentBinding.rb2.setChecked(true);
                } else {
                    mRefreshContentBinding.rb1.setVisibility(View.VISIBLE);
                    mRefreshContentBinding.rb1.setChecked(true);
                    mRefreshContentBinding.rb2.setChecked(false);
                }
            }
            mSid = getIntent().getStringExtra("sid");
        }
        setTitle("成员信息");
        mRefreshContentBinding.rv.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new SimpleBindingAdapter<SubordinateListBean.DataBean.ListBean, ItemMemberListAgentBinding>(R.layout.item_member_list_agent) {
            @Override
            public void bind(@NonNull ItemHolder<ItemMemberListAgentBinding> holder, final SubordinateListBean.DataBean.ListBean bean) {

                    holder.getBinding().cardRoot.setCardElevation(8);
                    holder.getBinding().tvMemberCount.setVisibility(View.VISIBLE);
                    holder.getBinding().tvMemberCountDesc.setVisibility(View.VISIBLE);
                    holder.getBinding().bottomDivider.setVisibility(View.GONE);
                    mRefreshContentBinding.rv.setBackgroundColor(Color.TRANSPARENT);
                    RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) holder.getBinding().cardRoot.getLayoutParams();
                    layoutParams.topMargin = RxImageTool.dip2px(10);
                    holder.getBinding().cardRoot.setLayoutParams(layoutParams);
//                } else {
//                    RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) holder.getBinding().cardRoot.getLayoutParams();
//                    layoutParams.topMargin = 0;
//                    holder.getBinding().cardRoot.setLayoutParams(layoutParams);
//                    mRefreshContentBinding.rv.setBackgroundColor(Color.WHITE);
//                    holder.getBinding().cardRoot.setCardElevation(0);
//                    holder.getBinding().tvMemberCount.setVisibility(View.INVISIBLE);
//                    holder.getBinding().tvMemberCountDesc.setVisibility(View.INVISIBLE);
//                    holder.getBinding().bottomDivider.setVisibility(View.VISIBLE);
//                }

                if (mVm.type.get() == 2) { //代理商
                    holder.getBinding().tvMemberCount.setVisibility(View.VISIBLE);
                    holder.getBinding().tvMemberCountDesc.setVisibility(View.VISIBLE);
                } else {
                    holder.getBinding().tvMemberCount.setVisibility(View.INVISIBLE);
                    holder.getBinding().tvMemberCountDesc.setVisibility(View.INVISIBLE);
                }
                GlideHelper.loadImg(holder.getBinding().ivIcon, bean.head_img);
                holder.getBinding().tvName.setText(bean.real_name);
                holder.getBinding().tvCurAmount.setText(StringUtils.doubleFormat(bean.turnover));
                holder.getBinding().tvCurRate.setText(StringUtils.doubleFormat(bean.rate)+"%");
                holder.getBinding().tvMemberCount.setText(bean.lowerlevel);

//                跳转修改费率
                holder.getBinding().tvChangeRate.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int myRole = LocalHelper.getMyRole();
                        double myrate = Double.parseDouble(LocalHelper.getMyrate());
                        Bundle bundle = new Bundle();
                        bundle.putInt("myrole",myRole);
                        bundle.putDouble("myrate",myrate);
                        bundle.putInt("role",bean.level);
                        bundle.putDouble("rate", Double.parseDouble(bean.rate));
                        bundle.putString("sid", String.valueOf(bean.sid));
                        RouterHelper.getInstance().startAct(RouteTable.main_change_rate,bundle);
                    }
                });
            }
        };

        mMemberAdapter = new SimpleBindingAdapter<SubordinateListBean.DataBean.ListBean, ItemMemberListBinding>(R.layout.item_member_list) {
            @Override
            public void bind(@NonNull ItemHolder<ItemMemberListBinding> holder, final SubordinateListBean.DataBean.ListBean bean) {
                GlideHelper.loadImg(holder.getBinding().ivIcon, bean.head_img);
                holder.getBinding().tvName.setText(bean.real_name);
                holder.getBinding().tvCurAmount.setText(StringUtils.doubleFormat(bean.turnover));
                holder.getBinding().tvCurRate.setText(StringUtils.doubleFormat(bean.rate));


                holder.getBinding().tvChangeRate.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int myRole = LocalHelper.getMyRole();
                        double myrate = Double.parseDouble(LocalHelper.getMyrate());
                        Bundle bundle = new Bundle();
                        bundle.putInt("myrole",myRole);
                        bundle.putDouble("myrate",myrate);
                        bundle.putInt("role",bean.level);
                        bundle.putDouble("rate", Double.parseDouble(bean.rate));
                        bundle.putString("sid", String.valueOf(bean.sid));
                        RouterHelper.getInstance().startAct(RouteTable.main_change_rate,bundle);
                    }
                });
            }
        };
        mRefreshContentBinding.rv.setAdapter(mAdapter);
        mRefreshContentBinding.rgroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                getData(true);
            }
        });

    }

    @Override
    protected int initRefreshLayout() {
        return R.layout.activity_member_list;
    }

    @Override
    protected void getData(boolean refresh) {
        showLoading();
        int checkedRadioButtonId = mRefreshContentBinding.rgroup.getCheckedRadioButtonId();
        if (checkedRadioButtonId == R.id.rb1) { //代理商
            mVm.type.set(2);
        } else if (checkedRadioButtonId == R.id.rb2) { //普通用户
            mVm.type.set(1);
        } else {
            mVm.type.set(2);
        }
        NetUtil.sellerPost(new JavaParamsBuilder()
                .addCmd(Consts.CMD.subordinateList)
                .addP("type", String.valueOf(mVm.type.get()))
                .addP("phoneOrName", mVm.phoneOrName.get())
                .addP("startTime", String.valueOf(mVm.startTime.get()))
                .addP("endTime", String.valueOf(mVm.endTime.get()))
                .addP("sid", mSid)
                .build(), new SuccessCallback<SubordinateListBean>() {
            @Override
            public void onSuccess(SubordinateListBean subordinateListBean) {
                mAdapter.setNewData(subordinateListBean.data.list);
                mRefreshContentBinding.rv.scheduleLayoutAnimation();
                finishGetData();
                hideLoading();
            }

            @Override
            public void onComplete() {
                super.onComplete();
                hideLoading();
                finishGetData();
            }
        });
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        getData(true);
    }
}
